<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>手机验证码登录</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <link rel="stylesheet" type="text/css" href="../css/login.css" />
</head>
<body style="background-image:url('../image/login.jpg');background-size:100% 100%">
  <div class="login_box">
      <h1>登录</h1>
      <div class="form-control"><input class="login_input" placeholder="请输入手机号" id="phone" type='number'/></div>
      <div class="form-control"><input class="login_input" placeholder="请输入验证码" id="num"/><button class="btn btn-o" onclick="getCode()" id="codeBtn">获取验证码</button></div>
      <div class="form-control"><button class="login_btn" onclick="login()" tapmode >登录</button></div>
      <div class="form-control"><a href="login.html" class="href_white">密码登录</a></div>
      <div class="form-control"><a href="register.html" class="href_white">没有帐号，立刻注册</a></div>
  </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery.js"></script>
<script type="text/javascript" src="../script/main.js"></script>
<script type="text/javascript">
apiready=function(){

}


function login(){
  var phone = $('#phone').val();
  var code = $('#num').val();
  var deviceId = api.deviceId;
  //账号密码都不能为空
  if (phone!=""&&code!="") {
    api.ajax({
      url: 'http://47.104.170.122/v1/portal/auth/loginByCode?phone='+phone+'&code='+code,
      method: 'POST',
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Authorization",
        "Content-Type" : 'application/json; charset=utf-8'
      }
    }, function(ret, err) {
        if (ret.code==0) {
          toast("登陆成功","bottom");
          var jwt=ret.data;
          //获取账号信息
          api.ajax({
            url: 'http://47.104.170.122/v1/portal/auth/app/loginInfo?token='+jwt,
            method: 'GET',
            headers: {
              "Access-Control-Allow-Origin": "*",
              "Access-Control-Allow-Headers": "Authorization",
              "Content-Type" : 'application/json; charset=utf-8'
            }
          }, function(ret, err) {
            if(ret.code==0){
            //存储aid和jwt
            //$api.setStorage('deviceId', deviceId);
            $api.setStorage('jwt', jwt);
            $api.setStorage('aid', ret.data.aid);
            $api.setStorage('flag', ret.data.flag);
            $api.setStorage('nickname', ret.data.nickname);
            $api.setStorage('userHeaderImageUrl', ret.data.userHeaderImageUrl);
            api.openFrame({
                        name: 'main',
                        url: 'main.html',
                        bounces: false,
                        rect: {
                            x: 0,
                            y: 0,
                            w: 'auto',
                            h: api.winHeight
                        }
                    })
                }
          });
        } else {
          toast("登陆失败","bottom");
        }
    });
    }else {
        api.toast({
            msg: '请填写相关信息',
            duration: 2000,
            location: 'bottom'
        })
    }
}
function loop(t,iTime){
  if(t>0){
    t=t-1;
    $("#codeBtn").html(t+'s后可重发');
    $("#codeBtn").attr('disabled','disabled');
    iTime=setTimeout(loop,1000,t,iTime);
  }else{
    clearTimeout(iTime);
    $("#codeBtn").html('重发验证码');
    t=6;
    $("#codeBtn").removeAttr('disabled');
  }
}
function getCode(){
  phone=$("#phone").val();
  if(!(/^1[34578]\d{9}$/.test(phone))){
    toast("手机号码有误","bottom");
    return false;
  }else{
    var t=60;
    var iTime;
    loop(t,iTime);
    //获取验证码接口
    api.ajax({
      url: 'http://47.104.170.122/v1/portal/auth/getCode?phone='+phone,
      method: 'GET',
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Authorization"
      }
    }, function(ret, err) {
      if(ret.code==0){

      }else{
        toast("获取验证码失败，请重新发送",bottom);
      }
    });
  }
}
</script>
</html>
